define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'project/project/index' + location.search,
                    add_url: 'project/project/add',
                    edit_url: 'project/project/edit',
                    del_url: 'project/project/del',
                    multi_url: 'project/project/multi',
                    import_url: 'project/project/import',
                    table: 'project',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type_id', title: __('Type.type_name'),  searchList: typeList,
                            formatter:function (value,row){
                                if (row.type_id > 0){
                                    return typeList[value];
                                }else{
                                    return '';
                                }

                            }
                        },
                        {field: 'project_name', title: __('Project_name'), operate: 'LIKE'},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'subject_id', title: __('Subject_id')},
                        // {field: 'type_id', title: __('Type_id')},
                        {field: 'subject.subject_name', title: __('Subject.subject_name'), operate: 'LIKE'},
                        {field: 'zj_num', title: __('zj_num'),operate: false},
                        {field: 'zj_num_mwh', title: __('zj_num_mwh'),operate: false},
                        {field: 'pay_status', title: __('pay_status'),searchList: payStatusList ,
                                formatter:function (value,row){
                                if (row.pay_status > 0){
                                    return payStatusList[value];
                                }else{
                                    return '';
                                }

                            }},
                        {field: 'pay_money', title: __('pay_money'),operate: false},
                        {field: 'filings_status', title: __('filings_status'),searchList: filingsStatusList ,
                            formatter:function (value,row){
                                if (row.filings_status > 0){
                                    return filingsStatusList[value];
                                }else{
                                    return '';
                                }

                            }},
                        {field: 'approval_status', title: __('approval_status'),searchList: approvalStatusList ,
                            formatter:function (value,row){
                                if (row.approval_status > 0){
                                    return approvalStatusList[value];
                                }else{
                                    return '';
                                }

                            }},
                        {field: 'area_id', title: __('area_id'),searchList: areaList ,
                            formatter:function (value,row){
                                if (row.area_id > 0){
                                    return areaList[value];
                                }else{
                                    return '';
                                }

                            }},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'project_no', title: __('Project_no'), operate: 'LIKE'},
                        {field: 'project_leader', title: __('Project_leader'), operate: 'LIKE'},
                        {field: 'pocket_unit', title: __('Pocket_unit'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'is_lx', title: __('Is_lx'),searchList: {"0":__('Is_lx 0'),"1":__('Is_lx 1')}},
                        {field: 'upload_time', title: __('Upload_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            formatter: function(value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if (row.type_id !== 6) {
                                    $(table).data("operate-edit", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                            buttons: [
                                {
                                    name: 'edit',
                                    text: "指派",
                                    icon: 'fa fa-pencil',
                                    title: "指派",
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-success btn-editone'
                                },
                                {
                                    name: 'detail',
                                    text: __('Detail'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'project/project/detail'
                                },
                            ],
                            // formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
